package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.view.View;
import android.widget.*;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.common.Constants;
import com.aduerr.v1shouyin.common.DatabaseManager;
import com.aduerr.v1shouyin.entity.OrderRecordEntity;
import com.aduerr.v1shouyin.entity.PreAuthoOrderEntity;
import com.aduerr.v1shouyin.entity.TransactionFlowEntity;
import com.aduerr.v1shouyin.http.APIRetrofit;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.print.PrintObserver;
import com.aduerr.v1shouyin.print.PrinterWrapper;
import com.aduerr.v1shouyin.unioncard.BaseUnionActivity;
import com.aduerr.v1shouyin.util.NumUtil;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.google.gson.Gson;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

import java.util.HashMap;


/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2019/4/15 19:19
 * @change
 * @chang time
 * @class describe
 */

public class PreAuthoOrderActivity extends BaseUnionActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_order_state)
    TextView tvOrderState;
    @BindView(R.id.iv_order_state)
    ImageView ivOrderState;
    @BindView(R.id.tv_order_id_01)
    TextView tvOrderId01;
    @BindView(R.id.tv_create_time_01)
    TextView tvCreateTime01;
    @BindView(R.id.tv_goods_money_01)
    TextView tvGoodsMoney01;
    @BindView(R.id.tv_deposit_01)
    TextView tvDeposit01;
    @BindView(R.id.tv_order_money_01)
    TextView tvOrderMoney01;
    @BindView(R.id.tv_shop_cut_01)
    TextView tvShopCut01;
    @BindView(R.id.tv_pay_money_01)
    TextView tvPayMoney01;
    @BindView(R.id.tv_pay_time_01)
    TextView tvPayTime01;
    @BindView(R.id.tv_order_remark_01)
    TextView tvOrderRemark01;
    @BindView(R.id.tv_shop_name_01)
    TextView tvShopName01;
    @BindView(R.id.tv_cashier_01)
    TextView tvCashier01;
    @BindView(R.id.tv_trans_id_01)
    TextView tvTransId01;
    @BindView(R.id.include_01)
    LinearLayout include01;
    @BindView(R.id.tv_deposit_use_02)
    TextView tvDepositUse02;
    @BindView(R.id.tv_refund_02)
    TextView tvRefund02;
    @BindView(R.id.tv_real_receive_02)
    TextView tvRealReceive02;
    @BindView(R.id.tv_operater_02)
    TextView tvOperater02;
    @BindView(R.id.tv_refund_time_02)
    TextView tvRefundTime02;
    @BindView(R.id.tv_refund_remark_02)
    TextView tvRefundRemark02;
    @BindView(R.id.tv_order_id_02)
    TextView tvOrderId02;
    @BindView(R.id.tv_create_time_02)
    TextView tvCreateTime02;
    @BindView(R.id.tv_goods_money_02)
    TextView tvGoodsMoney02;
    @BindView(R.id.tv_deposit_02)
    TextView tvDeposit02;
    @BindView(R.id.tv_order_money_02)
    TextView tvOrderMoney02;
    @BindView(R.id.tv_shop_cut_02)
    TextView tvShopCut02;
    @BindView(R.id.tv_pay_money_02)
    TextView tvPayMoney02;
    @BindView(R.id.tv_pay_time_02)
    TextView tvPayTime02;
    @BindView(R.id.tv_order_remark_02)
    TextView tvOrderRemark02;
    @BindView(R.id.tv_shop_name_02)
    TextView tvShopName02;
    @BindView(R.id.tv_cashier_02)
    TextView tvCashier02;
    @BindView(R.id.tv_trans_id_02)
    TextView tvTransId02;
    @BindView(R.id.include_02)
    LinearLayout include02;
    @BindView(R.id.tv_refund_03)
    TextView tvRefund03;
    @BindView(R.id.tv_real_receive_03)
    TextView tvRealReceive03;
    @BindView(R.id.tv_operater_03)
    TextView tvOperater03;
    @BindView(R.id.tv_refund_time_03)
    TextView tvRefundTime03;
    @BindView(R.id.tv_refund_remark_03)
    TextView tvRefundRemark03;
    @BindView(R.id.tv_order_id_03)
    TextView tvOrderId03;
    @BindView(R.id.tv_create_time_03)
    TextView tvCreateTime03;
    @BindView(R.id.tv_goods_money_03)
    TextView tvGoodsMoney03;
    @BindView(R.id.tv_deposit_03)
    TextView tvDeposit03;
    @BindView(R.id.tv_order_money_03)
    TextView tvOrderMoney03;
    @BindView(R.id.tv_shop_cut_03)
    TextView tvShopCut03;
    @BindView(R.id.tv_pay_money_03)
    TextView tvPayMoney03;
    @BindView(R.id.tv_pay_time_03)
    TextView tvPayTime03;
    @BindView(R.id.tv_order_remark_03)
    TextView tvOrderRemark03;
    @BindView(R.id.tv_shop_name_03)
    TextView tvShopName03;
    @BindView(R.id.tv_cashier_03)
    TextView tvCashier03;
    @BindView(R.id.tv_trans_id_03)
    TextView tvTransId03;
    @BindView(R.id.include_03)
    LinearLayout include03;
    @BindView(R.id.tv_cancel_04)
    TextView tvCancel04;
    @BindView(R.id.tv_cancel_real_04)
    TextView tvCancelReal04;
    @BindView(R.id.tv_cancel_operater_04)
    TextView tvCancelOperater04;
    @BindView(R.id.tv_cancel_time_04)
    TextView tvCancelTime04;
    @BindView(R.id.tv_cancel_remark_04)
    TextView tvCancelRemark04;
    @BindView(R.id.tv_deposit_use_04)
    TextView tvDepositUse04;
    @BindView(R.id.tv_refund_04)
    TextView tvRefund04;
    @BindView(R.id.tv_real_receive_04)
    TextView tvRealReceive04;
    @BindView(R.id.tv_operater_04)
    TextView tvOperater04;
    @BindView(R.id.tv_refund_time_04)
    TextView tvRefundTime04;
    @BindView(R.id.tv_refund_remark_04)
    TextView tvRefundRemark04;
    @BindView(R.id.tv_order_id_04)
    TextView tvOrderId04;
    @BindView(R.id.tv_create_time_04)
    TextView tvCreateTime04;
    @BindView(R.id.tv_goods_money_04)
    TextView tvGoodsMoney04;
    @BindView(R.id.tv_deposit_04)
    TextView tvDeposit04;
    @BindView(R.id.tv_order_money_04)
    TextView tvOrderMoney04;
    @BindView(R.id.tv_shop_cut_04)
    TextView tvShopCut04;
    @BindView(R.id.tv_pay_money_04)
    TextView tvPayMoney04;
    @BindView(R.id.tv_pay_time_04)
    TextView tvPayTime04;
    @BindView(R.id.tv_order_remark_04)
    TextView tvOrderRemark04;
    @BindView(R.id.tv_shop_name_04)
    TextView tvShopName04;
    @BindView(R.id.tv_cashier_04)
    TextView tvCashier04;
    @BindView(R.id.tv_trans_id_04)
    TextView tvTransId04;
    @BindView(R.id.include_04)
    LinearLayout include04;
    @BindView(R.id.bt_cancel)
    Button btCancel;
    @BindView(R.id.bt_refund)
    Button btRefund;
    @BindView(R.id.tv_pay_type_01)
    TextView tvPayType01;
    @BindView(R.id.tv_pay_type_02)
    TextView tvPayType02;
    @BindView(R.id.tv_pay_type_03)
    TextView tvPayType03;
    @BindView(R.id.tv_pay_type_04)
    TextView tvPayType04;
    @BindView(R.id.ll_pay_type_01)
    LinearLayout llPayType01;
    @BindView(R.id.ll_pay_money_01)
    LinearLayout llPayMoney01;

    String tag = "";
    String orderId = "";
    private PreAuthoOrderEntity.DataBean orderDetail;
    private boolean first = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_pre_autho_order;
    }

    @Override
    protected void initBase() {
        super.initView();
        ButterKnife.bind(this);
        tvTitle.setText("订单详情");
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("打印小票");
        tag = getIntent().getStringExtra("tag");
        orderId = getIntent().getStringExtra("orderid");
    }


    @Override
    protected void onResume() {
        super.onResume();
        getOrderDetail(orderId);
    }

    private void getOrderDetail(String orderId) {

        HashMap<String, String> addParams = new HashMap<>();
        addParams.put("OrderID", orderId);
        APIRetrofit.getAPIService().getPreAuthoOrder(RXRequest.getXingPosParams(addParams
                , this))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(entity -> {
                    if(entity.getSuccess() == 1) {
                        orderDetail = entity.getData();
                        fillData(entity.getData());
                        if(orderDetail.getOrderState() == 0) return;
                        if(tag.equals("result") && first) {
                            OrderRecordEntity order = new OrderRecordEntity();
                            TransactionFlowEntity tfEntity = new TransactionFlowEntity();

                            //0未支付1预授权冻资中2预授权完成3预授权撤销4预授权完成撤销
                            if(orderDetail.getOrderState() == 0) {
                                return;
                            } else if(orderDetail.getOrderState() == 1) {
                                order.setType(14);
                                tfEntity.setPrintType(4);
                            } else if(orderDetail.getOrderState() == 2) {
                                order.setType(15);
                                tfEntity.setPrintType(5);
                            } else if(orderDetail.getOrderState() == 3) {
                                order.setType(16);
                                tfEntity.setPrintType(6);
                            } else if(orderDetail.getOrderState() == 4) {
                                order.setType(17);
                                tfEntity.setPrintType(7);
                            }
                            order.setmConsumeMoney(orderDetail.getConsumeMoney());
                            order.setmRefundRemark(orderDetail.getConsumeRemark());
                            order.setmRefundTime(orderDetail.getConsumeTime());
                            order.setmConsumeTime(orderDetail.getConsumeTime());
                            order.setmCreateTime(orderDetail.getCreateTime());
                            order.setmDepositMoney(orderDetail.getDepositMoney());
                            order.setmGoodsMoney(orderDetail.getGoodsMoney());
                            order.setmOrderID(orderDetail.getOrderID());
                            order.setmOrderState(orderDetail.getOrderState());
                            order.setmPayTime(orderDetail.getPayTime());
                            order.setmPayType(orderDetail.getPayType());
                            order.setmRefundRemark(orderDetail.getRefundRemark());
                            order.setmRefundTime(orderDetail.getRefundTime());
                            order.setmRefundUserID(orderDetail.getRefundUserID());
                            order.setmRefundUserName(orderDetail.getRefundUserName());
                            order.setmShopID(orderDetail.getShopID());
                            order.setmShopName(orderDetail.getShopName());
                            order.setmSiteUserID(orderDetail.getSiteUserID());
                            order.setmSiteUserName(orderDetail.getSiteUserName());
                            order.setmTransactionID(orderDetail.getTransactionID());
                            order.setmConsumeUserName(orderDetail.getConsumeUserName());
                            order.setmCreateRemark(orderDetail.getCreateRemark());
                            DatabaseManager.getInstance().insert(order);
                            String json  = new Gson().toJson(orderDetail);
                            tfEntity.setJson(json);
                            DatabaseManager.getInstance().insert(tfEntity);
                            printText(entity.getData(),true);
                            first = false;
                        }
                    } else {
                        ToastUtils.showToast(this, entity.getErrMsg());
                        finish();
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                    ToastUtils.showToast(this, "网络异常");
                    finish();
                });

    }

    private void fillData(PreAuthoOrderEntity.DataBean data) {
        if(data.getOrderState() == 0) { //未支付
            tvRight.setVisibility(View.GONE);
            include01.setVisibility(View.VISIBLE);
            btCancel.setVisibility(View.GONE);
            btRefund.setVisibility(View.GONE);
            llPayType01.setVisibility(View.GONE);
            llPayMoney01.setVisibility(View.GONE);
//            tvPayType01.setText("未支付");
            tvOrderState.setText("未支付");
            ivOrderState.setImageResource(R.drawable.ic_pre_autho_money_01);

            tvOrderId01.setText(data.getOrderID());
            tvCreateTime01.setText(data.getCreateTime());
            tvGoodsMoney01.setText(data.getGoodsMoney() + "");
            tvDeposit01.setText(data.getDepositMoney() + "");
            tvOrderMoney01.setText(NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney()) + "");
            tvShopCut01.setText("0.00");
            tvPayMoney01.setText("0.00");
            tvPayTime01.setText("");
            tvShopName01.setText(data.getShopName() == null ? "" : data.getShopName());
            tvCashier01.setText(data.getSiteUserName());
            tvTransId01.setText(data.getTransactionID());
            tvOrderRemark01.setText(data.getCreateRemark());
        } else if(data.getOrderState() == 1) { //预授权冻资中
            include01.setVisibility(View.VISIBLE);
            btCancel.setVisibility(View.VISIBLE);
            btRefund.setVisibility(View.VISIBLE);
            tvOrderState.setText("预授权冻资中");
            ivOrderState.setImageResource(R.drawable.ic_pre_autho_money_02);

            tvOrderId01.setText(data.getOrderID());
            tvCreateTime01.setText(data.getCreateTime());
            tvGoodsMoney01.setText(data.getGoodsMoney() + "");
            tvDeposit01.setText(data.getDepositMoney() + "");
            tvOrderMoney01.setText(NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney()) + "");
            tvShopCut01.setText("0.00");
            tvPayMoney01.setText(NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney()) + "");
            tvPayTime01.setText(data.getPayTime());
            tvShopName01.setText(data.getShopName() == null ? "" : data.getShopName());
            tvCashier01.setText(data.getSiteUserName());
            tvTransId01.setText(data.getTransactionID());
            tvOrderRemark01.setText(data.getCreateRemark());
            tvPayType01.setText(data.getPayType() == 0 ? "微信" : "支付宝");
        } else if(data.getOrderState() == 2) { //预授权完成
            include02.setVisibility(View.VISIBLE);
            btCancel.setVisibility(View.VISIBLE);
            btRefund.setVisibility(View.GONE);
            tvOrderState.setText("预授权完成");
            ivOrderState.setImageResource(R.drawable.ic_pre_autho_money_03);

            tvDepositUse02.setText(NumUtil.formatFloat(data.getConsumeMoney() - data.getGoodsMoney()) + "");
            tvRefund02.setText("-" + NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney() - data.getConsumeMoney()));
            tvRealReceive02.setText(data.getConsumeMoney() + "");
            tvOperater02.setText(data.getConsumeUserName());
            tvRefundTime02.setText(data.getConsumeTime());
            tvRefundRemark02.setText(data.getConsumeRemark());

            tvOrderId02.setText(data.getOrderID());
            tvCreateTime02.setText(data.getCreateTime());
            tvGoodsMoney02.setText(data.getGoodsMoney() + "");
            tvDeposit02.setText(data.getDepositMoney() + "");
            tvOrderMoney02.setText(NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney()));
            tvShopCut02.setText("0.00");
            tvPayMoney02.setText(NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney()));
            tvPayTime02.setText(data.getPayTime());
            tvShopName02.setText(data.getShopName() == null ? "" : data.getShopName());
            tvCashier02.setText(data.getSiteUserName());
            tvTransId02.setText(data.getTransactionID());
            tvOrderRemark02.setText(data.getCreateRemark());
            tvPayType02.setText(data.getPayType() == 0 ? "微信" : "支付宝");
        } else if(data.getOrderState() == 3) { //预授权撤销
            include03.setVisibility(View.VISIBLE);
            btCancel.setVisibility(View.GONE);
            btRefund.setVisibility(View.GONE);
            tvOrderState.setText("预授权撤销");
            ivOrderState.setImageResource(R.drawable.ic_pre_autho_money_04);

            tvRefund03.setText("-" + NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney()));
            tvRealReceive03.setText("0.00");
            tvOperater03.setText(data.getRefundUserName());
            tvRefundTime03.setText(data.getRefundTime());
            tvRefundRemark03.setText(data.getRefundRemark());

            tvOrderId03.setText(data.getOrderID());
            tvCreateTime03.setText(data.getCreateTime());
            tvGoodsMoney03.setText(data.getGoodsMoney() + "");
            tvDeposit03.setText(data.getDepositMoney() + "");
            tvOrderMoney03.setText(NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney()));
            tvShopCut03.setText("0.00");
            tvPayMoney03.setText(NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney()));
            tvPayTime03.setText(data.getPayTime());
            tvShopName03.setText(data.getShopName() == null ? "" : data.getShopName());
            tvCashier03.setText(data.getSiteUserName());
            tvTransId03.setText(data.getTransactionID());
            tvOrderRemark03.setText(data.getCreateRemark());
            tvPayType03.setText(data.getPayType() == 0 ? "微信" : "支付宝");

        } else if(data.getOrderState() == 4) { //预授权完成撤销
            include04.setVisibility(View.VISIBLE);
            btCancel.setVisibility(View.GONE);
            btRefund.setVisibility(View.GONE);
            tvOrderState.setText("预授权完成撤销");
            ivOrderState.setImageResource(R.drawable.ic_pre_autho_money_04);

            tvCancel04.setText("-" + data.getConsumeMoney());
            tvCancelReal04.setText("0.00");
            tvCancelOperater04.setText(data.getRefundUserName());
            tvCancelTime04.setText(data.getRefundTime());
            tvCancelRemark04.setText(data.getRefundRemark());

            tvDepositUse04.setText(NumUtil.formatFloat(data.getConsumeMoney() - data.getGoodsMoney()) + "");
            tvRefund04.setText("-" + NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney() - data.getConsumeMoney()));
            tvRealReceive04.setText(data.getConsumeMoney() + "");
            tvOperater04.setText(data.getConsumeUserName());
            tvRefundTime04.setText(data.getConsumeTime());
            tvRefundRemark04.setText(data.getConsumeRemark());

            tvOrderId04.setText(data.getOrderID());
            tvCreateTime04.setText(data.getCreateTime());
            tvGoodsMoney04.setText(data.getGoodsMoney() + "");
            tvDeposit04.setText(data.getDepositMoney() + "");
            tvOrderMoney04.setText(NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney()));
            tvShopCut04.setText("0.00");
            tvPayMoney04.setText(NumUtil.formatFloat(data.getGoodsMoney() + data.getDepositMoney()));
            tvPayTime04.setText(data.getPayTime());
            tvShopName04.setText(data.getShopName() == null ? "" : data.getShopName());
            tvCashier04.setText(data.getSiteUserName());
            tvTransId04.setText(data.getTransactionID());
            tvOrderRemark04.setText(data.getCreateRemark());
            tvPayType04.setText(data.getPayType() == 0 ? "微信" : "支付宝");

        }
    }

    @OnClick({R.id.img_back, R.id.tv_right, R.id.rl_title,
            R.id.bt_cancel, R.id.bt_refund})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_right:
                printText(orderDetail,false);
                break;
            case R.id.bt_cancel:
                Intent cancle = new Intent(PreAuthoOrderActivity.this,
                        PreAuthoRefundActivity.class);
                cancle.putExtra("goods_money", orderDetail.getGoodsMoney());
                cancle.putExtra("deposit", orderDetail.getDepositMoney());
                cancle.putExtra("order_id", orderDetail.getOrderID());
                cancle.putExtra("order_state", orderDetail.getOrderState());

                if(orderDetail.getOrderState() == 2) { //预授权完成
                    cancle.putExtra("refund_money",
                            orderDetail.getGoodsMoney() + orderDetail.getDepositMoney() - orderDetail.getConsumeMoney());
                }
                startActivity(cancle);
                finish();
                break;
            case R.id.bt_refund:
                Intent refund = new Intent(PreAuthoOrderActivity.this,
                        PreAuthoCompleteActivity.class);
                refund.putExtra("goods_money", orderDetail.getGoodsMoney());
                refund.putExtra("deposit", orderDetail.getDepositMoney());
                refund.putExtra("order_id", orderDetail.getOrderID());
                startActivity(refund);
                finish();
                break;
        }
    }

    public void printText(PreAuthoOrderEntity.DataBean dataBean,boolean isAuto) {

        int printpage =
                (int) SharedPreferencesUtils.getParam(PreAuthoOrderActivity.this,
                        "printpage", 1);
        if(printpage == 0) {
            ToastUtils.showToast(PreAuthoOrderActivity.this, "打印张数设置为不打印,请先设置打印张数!!");
            return;
        }
        if(dataBean != null) {
            if(dataBean.getOrderState() == 1) {
                print(dataBean, Constants.PRINT_PRE_AUTHO_01, printpage,isAuto);
            } else if(dataBean.getOrderState() == 2) {
                print(dataBean, Constants.PRINT_PRE_AUTHO_02, printpage,isAuto);
            } else if(dataBean.getOrderState() == 3) {
                print(dataBean, Constants.PRINT_PRE_AUTHO_03, printpage,isAuto);
            } else if(dataBean.getOrderState() == 4) {
                print(dataBean, Constants.PRINT_PRE_AUTHO_04, printpage,isAuto);
            }
        }
    }

    private void print(PreAuthoOrderEntity.DataBean dataBean, int orderType,
                       int printPage,boolean isAuto) {
        printOrder(dataBean, orderType, printPage,isAuto);
    }

    private void printOrder(PreAuthoOrderEntity.DataBean dataBean,
                            int orderType, int printPage,boolean isAuto) {
        PrinterWrapper.printPreAuthoOrder(dataBean, orderType, printPage,isAuto);
    }
}
